package systems;

import helpers.GeneralHelper;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheManager;

public class RecentlyTag {
	
	private static String cacheName = "recentlyDomain";
	
	private static int limit = 500;
	
	private static List<String> tags = null;
	
	private static Cache cache = null;
	
	public static void insert(List<String> keywords) {
		
		cache = GeneralHelper.getCache();
		
		if (tags == null && cache != null) {
			try {
				tags = (List<String>) cache.get(cacheName);
			} catch (Exception e) {
			}
		}
		
		if (tags == null) {
			tags = new ArrayList<String>();
		}
		
		
		boolean isChanged = false;
		
		if (tags.size() < limit) {
			for (int i = 0; i < keywords.size(); i++) {
				if (!tags.contains(keywords.get(i)) && tags.size() < limit) {
					tags.add(keywords.get(i));
					isChanged = true;
				}
			}
		}
		
		if (isChanged) {
			cache.put(cacheName, tags);
		}
	}
	
	public static List<String> getTags() {
		
		cache = GeneralHelper.getCache();
		
		if(cache!=null && tags == null)
		{
			try {
				tags = (List<String>) cache.get(cacheName);
			} catch (Exception e) {
				// TODO: handle exception
			}
			
		}
		
		if (tags == null) {
			tags = new ArrayList<String>();
		}
		
		return tags;
	}
}
